package org.nokatag.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.nokatag.jdom.Document;
import org.nokatag.jdom.Element;
import org.nokatag.jdom.JDOMException;
import org.nokatag.jdom.ProcessingInstruction;
import org.nokatag.jdom.input.SAXBuilder;
import org.nokatag.jdom.output.Format;
import org.nokatag.jdom.output.XMLOutputter;

public class XmlWrite {
	

	
	@SuppressWarnings("unchecked")
	public static void writeXml(String fileid,String filename,String filecreatedate,String filepath,String filetype,String jsptimeid,String type ) throws Exception{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
		FileInputStream fileinputstream = new FileInputStream(file);
		Document doc = sb.build(fileinputstream);
		Element rootelement=doc.getRootElement();
		List childlist4 =rootelement.getChildren("file");
		Element es = new Element("file");
		es.setAttribute("fileid", fileid);
		es.setAttribute("filename", filename);
		es.setAttribute("filecreatedate", filecreatedate);
		es.setAttribute("filepath", filepath);
		es.setAttribute("filetype", filetype);
		es.setAttribute("jsptimeid", jsptimeid);
		es.setAttribute("selecttype",type);
		childlist4.add(es);
		savedate(doc,file,fileinputstream);
	}
	
	@SuppressWarnings("unchecked")
	public static String fileidToFilepath(String fileid) throws FileNotFoundException, JDOMException, IOException{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
		FileInputStream fileinputstream = new FileInputStream(file);
		String filepath = "";
		Document doc = sb.build(fileinputstream);
		Element rootelement=doc.getRootElement();
		List <Element>childlist4 =rootelement.getChildren("file");
		for(Element el:childlist4){
			if(el.getAttributeValue("fileid").toString().equals(fileid)){
				filepath=Concest.FILESERVERPATH+"/"+el.getAttributeValue("filepath").toString();
				break;
			}
		}
		fileinputstream.close();
		return filepath;
	}
	public static void xmlinit() throws FileNotFoundException, IOException{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		if(!file.exists()){
			File files = new File(Concest.FILESERVERPATH);
			files.mkdirs();
			Element root;  
	        root = new Element("fileinfo"); //生成根元素：student-info
	        Document doc = new Document(); //将根元素植入文档doc中
	        ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet","href=\"nokatag/datalist.xsl\" type=\"text/xsl\""); 
	        doc.addContent(pi);
	        doc.addContent(root);
	        Format format = Format.getCompactFormat();
	        format.setEncoding("utf-8"); //设置xml文件的字符为gb2312
	        format.setIndent("    "); //设置xml文件的缩进为4个空格
	        
	        XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格 
	        XMLOut.output(doc, new FileOutputStream(file));  
		}
	}
	@SuppressWarnings("unchecked")
	public static void xmldelchid(String[] fileids) throws Exception{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
		FileInputStream fileinputstream = new FileInputStream(file);
		Document doc = sb.build(fileinputstream);
		Element rootelement=doc.getRootElement();
		List <Element>childlist4 =rootelement.getChildren("file");
		
		for(String fileid:fileids){
			Iterator iterator= childlist4.iterator();
			while(iterator.hasNext()){
				Element el = (Element)iterator.next();
				if(el.getAttributeValue("fileid").toString().equals(fileid)){
					String filepath = Concest.FILESERVERPATH+"/"+el.getAttributeValue("filepath");
					File filedel = new File(filepath);
					File filedel2 = new File(filepath.substring(0,filepath.lastIndexOf("/")));
					filedel.delete();
					filedel2.delete();
					iterator.remove();
				}
			}
			//--------------从内存中删除-------------------
		}
		savedate(doc,file,fileinputstream);
	}
	
	@SuppressWarnings("unchecked")
	public static String fileidToFileNames(String fileids) throws FileNotFoundException, JDOMException, IOException{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
		FileInputStream fileinputstream = new FileInputStream(file);
		String filename = "";
		Document doc = sb.build(fileinputstream);
		Element rootelement=doc.getRootElement();
		List <Element>childlist4 =rootelement.getChildren("file");
		Long[] fileidlongs =  Function.stringTolongArry(fileids,",");
		for(Long fileid:fileidlongs)
		for(Element el:childlist4){
			if(el.getAttributeValue("fileid").toString().equals(String.valueOf(fileid))){
				filename+=el.getAttributeValue("filename").toString();
			}
		}
		fileinputstream.close();
		return filename;
	}
	
	@SuppressWarnings("unchecked")
	public static String fileidToFileNames(String fileids,Integer showtype,String s,String e,String basepath) throws FileNotFoundException, JDOMException, IOException{
		File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
		SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
		FileInputStream fileinputstream = new FileInputStream(file);
		StringBuilder bodystr = new StringBuilder();
		Document doc = sb.build(fileinputstream);
		Element rootelement=doc.getRootElement();
		List <Element>childlist4 =rootelement.getChildren("file");
		Long[] fileidlongs =  Function.stringTolongArry(fileids,",");
		for(Long fileid:fileidlongs)
		for(Element el:childlist4){
			if(el.getAttributeValue("fileid").toString().equals(String.valueOf(fileid))){
				switch(showtype){
				case 1:bodystr.append(s+"<a target=\"_black\" href='"+basepath+"/nokatag/nokatag_uploadfile/file.fs?do=get&fileid="+String.valueOf(fileid)+"'>"+el.getAttributeValue("filename").toString()+"</a>"+e+"\n");
				break;
				case 2:bodystr.append(s+"<a target=\"_black\" href='"+basepath+"/nokatag/nokatag_uploadfile/file.fs?do=red&fileid="+String.valueOf(fileid)+"'>"+el.getAttributeValue("filename").toString()+"</a>"+e+"\n");
				break;
				case 0:bodystr.append(s+el.getAttributeValue("filename").toString()+e+"\n");
				break;
			}
			}
		}
		fileinputstream.close();
		return bodystr.toString();
	}
	@SuppressWarnings("unchecked")
	public static List<Map<String,String>> list(){
		List<Map<String,String>> filelist = new ArrayList<Map<String,String>>();
		try{
			File file = new File(Concest.FILESERVERPATH+"/filedata.xml");
			SAXBuilder sb = new SAXBuilder(); // 新建立构造器 
			FileInputStream fileinputstream = new FileInputStream(file);
			Document doc = sb.build(fileinputstream);
			Element rootelement=doc.getRootElement();
			List <Element>childlist4 =rootelement.getChildren("file");
			for(Element el:childlist4){
				Map<String,String> map = new HashMap<String,String>();
				map.put("fileid", el.getAttributeValue("fileid"));
	            map.put("filename", el.getAttributeValue("filename"));
	            map.put("filecreatedate", el.getAttributeValue("filecreatedate"));
	            map.put("filepath", el.getAttributeValue("filepath"));
	            map.put("filetype", el.getAttributeValue("filetype"));
	            map.put("jsptimeid",el.getAttributeValue("jsptimeid"));
	            map.put("selecttype", el.getAttributeValue("selecttype"));
				filelist.add(map);
			}
			fileinputstream.close();
		}catch(FileNotFoundException e){
			 e.printStackTrace();
		}catch(JDOMException e){
			 e.printStackTrace();
		}catch(IOException e){
			 e.printStackTrace();
		}
		return filelist;
	}
	private static  void savedate(Document doc,File datafilename,FileInputStream fileinputstream)throws Exception{
    	FileOutputStream   out   =   new   FileOutputStream(datafilename);   
	    XMLOutputter   serializer   =   new   XMLOutputter();   
	    serializer.output(doc,   out);   
	    out.flush();   
	    out.close();
	    fileinputstream.close();
    }
	
	
	
}
